﻿using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_formulario : System.Web.UI.Page
{
    public ArrayList campos_arr = null;
    RE_User userbean = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        Hashtable ht = null;
        int id = 0;
        if (Session["userid"] == null) Response.Redirect("../default.aspx");
        userbean=(RE_User)Session["userid"];


        if (Request.QueryString["id"] != null && !Request.QueryString["id"].ToString().Equals(""))
        {
            id = int.Parse(Request.QueryString["id"].ToString());
            lb_id.Text = id.ToString();
            ht = (Hashtable)DB.getDatabyID(id);
        }


        ArrayList sections = DB.getSections(2);
        TableRow tr = null, trsection = null, tr2 = null;
        Table sectiontable = null;
        TableCell tc1 = null, tc2 = null, tc3 = null, tc4 = null, tc5 = null, tc6 = null, tcsection = null;
        Label lb_nombre = null;
        TextBox tb = null;
        RadioButtonList rbl = null;
        DropDownList ddl = null;
        Label lb = null;
        RequiredFieldValidator rfv = null;
        RegularExpressionValidator rev = null;
        ListItem item = null;
        ArrayList listado_arr = null;
        int contador = 0;
        TableHeaderCell tdtitulo = null;
        Label lbtitulo = null;
        foreach (RE_GenericBean section in sections)
        {
            tr = new TableRow();
            lbtitulo = new Label();
            lbtitulo.Text = section.strC1;
            tdtitulo = new TableHeaderCell();
            //tdtitulo.Style.Add("style", "font-weight: bold; color: white; background-color: #6b696b; height: 13px;");
            tdtitulo.Controls.Add(lbtitulo);
            tr.Cells.Add(tdtitulo);
            Tabla.Rows.Add(tr);
            tr2 = new TableRow();
            tcsection = new TableCell();

            tr2.HorizontalAlign = HorizontalAlign.Left;
            tcsection.VerticalAlign = VerticalAlign.Top;

            sectiontable = new Table();
            sectiontable.Attributes.Add("width", "100%");
            sectiontable.BorderStyle = BorderStyle.Groove;
            campos_arr = DB.getCamposFormulario(section.intC1.ToString());
            contador = 0;
            foreach (campos_Bean campo in campos_arr)
            {
                if (contador % section.intC2 == 0) trsection = new TableRow();
                contador++;
                tc1 = new TableCell();
                tc2 = new TableCell();
                
                tc1.HorizontalAlign = HorizontalAlign.Left;
                tc2.HorizontalAlign = HorizontalAlign.Left;               

                tc1.VerticalAlign = VerticalAlign.Top;
                tc2.VerticalAlign = VerticalAlign.Top;
                
                tc1.Width = 100;


                lb_nombre = new Label();
                lb_nombre.ID = "lb_" + campo.Nombre;
                lb_nombre.Text = campo.DisplayName;

                tc1.Controls.Add(lb_nombre);
                if (campo.TipoCampo.Equals("DropDown"))
                {
                    ddl = new DropDownList();
                    ddl.ID = campo.Nombre;
                    listado_arr = (ArrayList)DB.getListado(campo.TablaDep, campo.CampoDep, campo.WhereDep);
                    if ((listado_arr != null) && (listado_arr.Count > 0))
                    {
                        item = new ListItem("Select One", "");
                        ddl.Items.Add(item);
                        foreach (RE_GenericBean rgb in listado_arr)
                        {   
                            item = new ListItem(rgb.strC1, rgb.strC2);
                            ddl.Items.Add(item);
                        }
                        ddl.SelectedIndex = campo.SelectedIndex;
                        
                    }
                    if (campo.Nombre.Equals("frmAgentName") && userbean.Level==3) ddl.SelectedValue = userbean.Name + " " + userbean.LastName;
                    if (campo.Nombre.Equals("frmQCApproval") && userbean.Level == 1) ddl.SelectedValue = userbean.Name + " " + userbean.LastName; 
                    
                    if (ht != null)
                    {
                        if (ht.ContainsKey(ddl.ID.ToString()))
                            ddl.SelectedValue = ht[ddl.ID.ToString()].ToString().Trim();
                    }
                    if ((campo.Nombre.Equals("frmPSBPO_ClientE")))
                    {
                        ddl.SelectedValue = "Spark Energy";

                    }
                    
                    tc2.Controls.Add(ddl);
                    tc2.Controls.Add(new LiteralControl("<br />"));
                }
                else if (campo.TipoCampo.Equals("Option"))
                {
                    rbl = new RadioButtonList();
                    rbl.ID = campo.Nombre;
                    listado_arr = (ArrayList)DB.getListado(campo.TablaDep, campo.CampoDep, campo.WhereDep);
                    if ((listado_arr != null) && (listado_arr.Count > 0))
                    {

                        foreach (RE_GenericBean rgb in listado_arr)
                        {
                            item = new ListItem(rgb.strC1, rgb.strC2);
                            rbl.Items.Add(item);
                        }
                        rbl.SelectedIndex = campo.SelectedIndex;
                    }
                    
                    if (ht != null)
                    {
                        if (ht.ContainsKey(rbl.ID.ToString()))
                            rbl.SelectedValue = ht[rbl.ID.ToString()].ToString();
                    }
                    
                    tc2.Controls.Add(rbl);
                    tc2.Controls.Add(new LiteralControl("<br />"));
                }
                else if (campo.TipoCampo.Equals("Label"))
                {
                    lb = new Label();
                    lb.ID = campo.Nombre;
                    if ((campo.Nombre.Equals("frmPromo")))
                    {
                        lb.Text = "HMCDR02";

                    }
                    if ((campo.Nombre.Equals("frmIpAddress")))
                    {
                        lb.Text = "190.143.139.146";

                    }
                    lb.Font.Size = 10;
                    lb.Width = campo.Size;
                        
                    tc2.Controls.Add(lb);
                    tc2.Controls.Add(new LiteralControl("<br />"));
                    /* if (ht != null)
                        {
                            if (ht.ContainsKey(ddl.ID.ToString()))
                        //       lb.Text = ht[lb.ID.ToString()].ToString();
                        }*/
                }
                else
                {
                    tb = new TextBox();
                    tb.ID = campo.Nombre;
                    if (campo.Rows != null && campo.Rows > 1)
                    {
                        tb.TextMode = TextBoxMode.MultiLine;
                        tb.Rows = campo.Rows;
                    }

                    tb.Width = campo.Size;
                    tb.MaxLength = campo.MaxLen;
                        
                    tc2.Controls.Add(tb);
                    tc2.Controls.Add(new LiteralControl("<br />"));
                    if (campo.Nombre.Equals("frmDateIn") || campo.Nombre.Equals("frmQCDate")) {
                        tb.Text = DateTime.Now.ToString("MM/dd/yy");
                        tb.Enabled = false;
                    }
                    if (campo.Nombre.Equals("frmAppTime"))
                    {
                        tb.Text = DateTime.Now.ToString("h:mmtt");
                        tb.Enabled = false;
                    }

                    if (ht != null)
                    {
                        if (ht.ContainsKey(tb.ID.ToString()))
                            tb.Text = ht[tb.ID.ToString()].ToString();
                    }
                    if (campo.AllowNull == 0)
                    {
                        rfv = new RequiredFieldValidator();
                        rfv.ID = "rfv_" + campo.Nombre;
                        rfv.ErrorMessage = "This field is required";
                        rfv.ControlToValidate = campo.Nombre;
                        rfv.SetFocusOnError = true;
                        tc2.Controls.Add(rfv);
                        tc2.Controls.Add(new LiteralControl("<br />"));
                    }
                    if (campo.ValidaExpr == 1)
                    {
                        rev = new RegularExpressionValidator();
                        rev.ID = "rev_" + campo.Nombre;
                        if (campo.MessageError.Equals(""))
                            rev.ErrorMessage = "Incorrect Format";
                        else
                        {
                            rev.ErrorMessage = campo.MessageError;
                        }
                        rev.ControlToValidate = campo.Nombre;
                        rev.ValidationExpression = campo.Expresion_Reg;
                        tc2.Controls.Add(rev);
                    }
                    
                }
                trsection.Cells.Add(tc1);
                trsection.Cells.Add(tc2);
                sectiontable.Rows.Add(trsection);
            }
            tcsection.Controls.Add(sectiontable);
            tr2.Cells.Add(tcsection);
            Tabla.Rows.Add(tr2);
        }
    }
    protected void btn_save_search_Click(object sender, EventArgs e)
    {
        int respuesta = Guardar_datos();
        if (respuesta == -1)
        {
            msg.Text = "Error, try again please";
            return;
        }
        Response.Redirect("buscador.aspx");
    }

    private int Guardar_datos()
    {
        int result = 0;
        TextBox tb = null;
        DropDownList ddl = null;
        string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
        xml += "<campos>";

        foreach (Control row in Tabla.Controls)
        {
            foreach (Control cell in row.Controls)
            {
                foreach (Control tabla1 in cell.Controls)
                {
                    foreach (Control row1 in tabla1.Controls)
                    {
                        foreach (Control cell1 in row1.Controls)
                        {
                            foreach (Control child in cell1.Controls)
                            {

                                if (child is TextBox)
                                {
                                    tb = (TextBox)child;
                                    if (tb.Text != null && !tb.Text.Trim().Equals(""))
                                        xml += "<" + tb.ID + ">" + tb.Text.ToUpper() + "</"+tb.ID+">";
                                    else
                                        xml += "<" + tb.ID + " />";
                                }
                                else if ((child is DropDownList) || (child is RadioButtonList))
                                {
                                    ddl = (DropDownList)child;
                                    xml += "<" + ddl.ID + ">" + ddl.SelectedValue + "</"+ddl.ID+">";
                                }

                            }
                        }
                    }
                }
            }
        }
        xml += "</campos>";
        int id = int.Parse(lb_id.Text);

        return DB.InsetUpdateData(id, xml);
    }
    protected void bt_delete_Click(object sender, EventArgs e)
    {
        int id = int.Parse(lb_id.Text);
        if (id == 0)
        {
            msg.Text = "Error.";
            msg.Visible = true;
            return;
        }

        int result = DB.deleteData(id);
        if (result != 1)
        {
            msg.Text = "Error.  Can't delete this item, please try again";
            msg.Visible = true;
            return;
        }
        Response.Redirect("buscador.aspx");
    }
}
